package servlet;


import jdbc.StudentHomeworkJdbc;
import model.Homework;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/sh")
public class StudentHomeworkServlet extends HttpServlet {

    private static String account = null;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie[] cookies = req.getCookies();
        for(Cookie cookie: cookies){
            if(cookie.getName().equals("account")){
                account = cookie.getValue();
                System.out.println("student-homework-servlet:  "+ account);
            }
        }
        if(!account.equals("1111")){        //学生登录
            List<Homework> list = new ArrayList<>();
            int sign = 0;
            list = StudentHomeworkJdbc.get_student_homework(account);
            req.setAttribute("list", list);
            req.setAttribute("sign", sign);
            req.getRequestDispatcher("index.jsp").forward(req, resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String submit_info = req.getParameter("submit");
        System.out.println("student-homework-servlet-homework-submit:  " + submit_info);
        String[] info = submit_info.split(" ");
        String id = info[1];
        String title = req.getParameter("homework_title+"+id);
        String homework_content = req.getParameter("content+"+id);
        byte  b[ ]=homework_content.getBytes("ISO-8859-1");
        homework_content = new String(b);
        System.out.println("student-homework-servlet-homework-content:  " + homework_content);
        System.out.println("student-homework-servlet-homework-id:  "+ id);
        System.out.println("student-homework-servlet-homework-title:  " + title);
        StudentHomeworkJdbc.insert_student_homework(account, id, homework_content,title);
        doGet(req, resp);
    }
}
